<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();
        $this->load->model('authGroup_m');
        $this->load->library('pagination');
		if(!$this->auth->is_super_admin()){
			redirect(site_url('user/login'));
		}
	}
	
	public function index($page=1)
	{
        $keywords = $this->input->get('user_name');
        $limit = 30;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = TRUE;
        $config['base_url'] = base_url('manage/user/index/');
        $config['total_rows'] = $this->user_m->count_user_find($keywords);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $users = $this->user_m->get_user_group_rule_list($start,$limit,$keywords);
        $departmentid = explode('|', $this->config->item('department'));
        $department = explode('|', $this->config->item('department_text'));
        $data['department']=array_combine($departmentid,$department);
        $data['users'] = $users;
		$this->load->view('user/index',$data);
	}
	//新增成员
    public function add(){
        $departmentid = explode('|', $this->config->item('department'));
        $department = explode('|', $this->config->item('department_text'));
        $data['department']=array_combine($departmentid,$department);
        $usergroup = $this->authGroup_m->get_all_groups(1);
        $data['usergroup'] = $usergroup;
	    $this->load->view('user/form',$data);
    }
    //更新信息
    public function update(){
        $post = $this->input->post();
        $uid = isset($post['user_id']) ? intval($post['user_id']) : false;
        $username = isset($post['user_name']) ? htmlspecialchars($post['user_name'], ENT_QUOTES) : '';
        $group_id = isset($post['user_group']) ? intval($post['user_group']) : false;
        if (!$group_id) {
            show_message('请给用户选择用户组',site_url(X_URL.'user/add'),1);
        }
        $salt =get_salt();
        $getpassword= $this->input->post('user_password',true);
        if (!$uid) {
            $admin = array(
                'user_name' => $username,
                'user_uname' =>$post['user_uname'],
                'user_password' =>  password_dohash($getpassword,$salt),
                'user_salt' => $salt,
                'user_money'=> 0,
                'user_group'=> $group_id,
                'user_regtime' => time(),
                'user_logtime' => time(),
                'user_lock'=>0,
                'user_tel' =>$post['user_tel'],
                'user_qq' =>$post['user_qq'],
                'user_wechat' =>$post['user_wechat'],
                'user_department' =>$post['user_department'],
            );

            if(!$this->user_m->check_register_username($username)){
                $this->user_m->register($admin);
                $new_id = $this->db->insert_id();
            }

            $this->user_m->update_user($new_id,array('user_key'=>password_dohash($new_id,$salt)));
            $this->groupAcess_m->insert_user_access(['uid'=>$new_id,'group_id'=>$group_id]);
            show_message('新增成员成功',site_url(X_URL.'user/index'),1);
        } else {
            // 修改
            $admin = array(
                'user_group'=> $group_id,
                'user_tel' =>$post['user_tel'],
                'user_qq' =>$post['user_qq'],
                'user_wechat' =>$post['user_wechat'],
                'user_department' =>$post['user_department'],
            );
            if(!empty($getpassword)){
                $admin['user_salt'] = $salt;
                $admin['user_password'] = password_dohash($getpassword,$salt);
            }
            if(!empty($username)){
                $admin['user_name'] = $username;
            }
            $this->user_m->update_user($uid,$admin);
            $this->groupAcess_m->update_user_access($uid,['group_id'=>$group_id]);
            show_message('修改成员成功',site_url(X_URL.'user/index'),1);

        }
    }
    public function edit($user_id){
        if ($user_id) {
            $users = $this->user_m->get_user_by_user_id_group($user_id);
        } else {
            show_message('参数错误',site_url(X_URL.'user/index'),1);
        }
        $usergroup = $this->authGroup_m->get_all_groups(1);
        $data['usergroup'] = $usergroup;
        $data['users'] = $users;
        $departmentid = explode('|', $this->config->item('department'));
        $department = explode('|', $this->config->item('department_text'));
        $data['department']=array_combine($departmentid,$department);
        $this->load->view('user/form',$data);
    }

    public function delete($user_id){
        if($user_id){
            $result = $this->user_m->update_user($user_id,array('delete'=>1));
            if($result){
                show_message('删除成功',site_url(X_URL.'user/index'),1);
            }else{
                show_message('删除失败',site_url(X_URL.'user/index'),1);
            }
        }else{
            show_message('参数错误',site_url(X_URL.'user/index'),1);
        }
    }


}